home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Inside Mac Games Volume 3 #12
/
IMG 33 Dec 1995.iso
/
Important Files
/
MGW1Codeƒ
/
MGWGraphics1.c
< prev
next >
Wrap
Text File
|
1995-06-26
|
7KB
|
201 lines
//==============================================================================================\\
// ----------------------------------------------------------------------------------- \\
// MGWGraphics1.c version 1.0.0 copyright © 1993…1995 Jamie McCornack, john calhoun \\
// ----------------------------------------------------------------------------------- \\
// Graphics utilities for Macintosh GameWriter 1.0.0, a training program… \\
// …for beginning Mac game programmers. MGW1 includes MGWExterns1.h, MGWUtilities1.c,… \\
// …MGWSound1.c, MGWGraphics1.c, MGWGraphicsBWLite1.c, HelloWorld.rsrc and an assortment… \\
// …of demo programs; projects HelloWorld1.π etc. and source code files HelloWorld1.c etc. \\
// A tutorial is available in Tricks of the Mac Game Programming Gurus, published… \\
// …by Hayden Books, August 1995. \\
// \\
// This code is offered by the copyright holders for no fee and for whatever use… \\
// …you care to make of it, but we do hope you remember where it came from. \\
// \\
// Please send bug reports to MacGameDev at America OnLine. macgamedev@aol.com \\
// Suggestions and observations are also appreciated. \\
// Updates and upgrades will be available now and then from the above e-mail address. \\
//==============================================================================================\\
#include "MGWExterns1.h"
extern GDHandle gThisGDevice;
extern GrafPtr mainWindow, workCPort; // Your program may use different names.
// See comments, DumpScreenToWork().
//============================================================== Functions
//-------------------------------------------------------------- CreateOffScreenBitMap
// Checks for sufficient memory, then creates an offscreen bitmap.
// If there is not sufficient memory, alerts the user and quits to Finder.
void CreateOffScreenBitMap (Rect *theRect, GrafPtr *offScreen)
{
GrafPtr theBWPort;
BitMap theBitMap;
long theRowBytes;
theBWPort = (GrafPtr)(NewPtr(sizeof(GrafPort)));
OpenPort(theBWPort);
theRowBytes = (long)((theRect->right - theRect->left + 15L) / 16L) * 2L;
theBitMap.rowBytes = (short)theRowBytes;
theBitMap.baseAddr = NewPtr((long)theBitMap.rowBytes *
(theRect->bottom - theRect->top));
if (theBitMap.baseAddr == 0L)
RedAlert(kErrLowMemory);
theBitMap.bounds = *theRect;
if (MemError() != noErr)
RedAlert(kErrLowMemory);
SetPortBits(&theBitMap);
ClipRect(theRect);
RectRgn(theBWPort->visRgn, theRect);
EraseRect(theRect);
*offScreen = theBWPort;
}
//-------------------------------------------------------------- CreateOffScreenPixMap
// Handles the creation of an offscreen pixmap. Depth is assumed to be that of the…
// current gDevice. If the allocation fails (low memory, etc.) we quit to Finder.
void CreateOffScreenPixMap (Rect *theRect, CGrafPtr *offScreen)
{
CTabHandle thisColorTable;
GDHandle oldDevice;
CGrafPtr newCGrafPtr;
Ptr theseBits;
long sizeOfOff, offRowBytes;
OSErr theErr;
short thisDepth;
gThisGDevice = GetMainDevice();
oldDevice = GetGDevice();
SetGDevice(gThisGDevice);
newCGrafPtr = 0L;
newCGrafPtr = (CGrafPtr)NewPtrClear(sizeof(CGrafPort));
if (newCGrafPtr != 0L)
{
OpenCPort(newCGrafPtr);
thisDepth = (**(*newCGrafPtr).portPixMap).pixelSize;
offRowBytes = ((((long)thisDepth *
(long)(theRect->right - theRect->left)) + 15L) >> 4L) << 1L;
sizeOfOff = (long)(theRect->bottom - theRect->top) * offRowBytes;
OffsetRect(theRect, -theRect->left, -theRect->top);
theseBits = NewPtr(sizeOfOff);
if (theseBits != 0L)
{
(**(*newCGrafPtr).portPixMap).baseAddr = theseBits;
(**(*newCGrafPtr).portPixMap).rowBytes = (short)offRowBytes + 0x8000;
(**(*newCGrafPtr).portPixMap).bounds = *theRect;
thisColorTable = (**(**gThisGDevice).gdPMap).pmTable;
theErr = HandToHand((Handle *)&thisColorTable);
(**(*newCGrafPtr).portPixMap).pmTable = thisColorTable;
ClipRect(theRect);
RectRgn(newCGrafPtr->visRgn, theRect);
ForeColor(blackColor);
BackColor(whiteColor);
EraseRect(theRect);
}
else
{
CloseCPort(newCGrafPtr);
DisposePtr((Ptr)newCGrafPtr);
newCGrafPtr = 0L;
RedAlert(kErrLowMemory);
}
}
else
RedAlert(kErrLowMemory);
*offScreen = newCGrafPtr;
SetGDevice(oldDevice);
}
//-------------------------------------------------------------- KillOffScreenBitMap
// Disposes of an offscreen bitmap, and frees the memory for other purposes.
void KillOffscreenBitMap (GrafPtr *wasPort)
{
if (wasPort != nil)
{
ClosePort(*wasPort);
*wasPort = nil;
}
}
//-------------------------------------------------------------- KillOffScreenPixMap
// Disposes of an offscreen pixMap, and frees the memory for other purposes.
void KillOffscreenPixMap (CGrafPtr *wasPort)
{
if (wasPort != nil)
{
CloseCPort(*wasPort);
*wasPort = nil;
}
}
//-------------------------------------------------------------- LoadGraphic
// Handy function that loads a PICT graphic, get's its bounds and draws it.
// The port drawn to is assumed the current port. No scaling is done.
void LoadGraphic (short thePictID)
{
Rect bounds;
PicHandle thePicture;
thePicture = GetPicture(thePictID); // Load graphic from resource fork.
if (thePicture == 0L) // Check to see if nil (did it load?)
RedAlert(kErrNoResource);
HLock((Handle)thePicture); // If we made it this far, lock handle.
bounds = (*thePicture)->picFrame; // Get a copy of the picture's bounds.
HUnlock((Handle)thePicture); // We can unlock the picture now.
OffsetRect(&bounds, -bounds.left, -bounds.top); // Offset bounds rect to (0, 0).
DrawPicture(thePicture, &bounds); // Draw picture to current port.
ReleaseResource((Handle)thePicture); // Dispose of picture from heap.
}
//-------------------------------------------------------------- DumpWorkToScreen
// Simple handy function that copies the entire work pixmap to the screen.
// Of course, you'll have to rename the CopyBits parameters to fit your own program,…
// …unless you habitually name your main window mainWindow and your offscreen…
// …graphics buffer workCPort. Generally, you'll want to set targetRect to the full…
// …clipping rect of the main window.
void DumpWorkToScreen (Rect targetRect)
{
CopyBits(&(((GrafPtr)workCPort)->portBits),
&((GrafPtr)mainWindow)->portBits,
&targetRect, &targetRect, srcCopy, 0L);
}
//-------------------------------------------------------------- DumpScreenToWork
// Simple handy function that copies the entire screen to the work pixmap.
// The reverse of the above routine. Very handy routines for when you have alerts,…
// …or anything else that temporarily obscures the main window. Call DumpWorkToScreen()…
// …just before the alert, and DumpScreenToWork immediately after the alert.
// Oh yeah, and use the same targetRect for both routines.
void DumpScreenToWork (Rect targetRect)
{
CopyBits(&(((GrafPtr)mainWindow)->portBits),
&((GrafPtr)workCPort)->portBits,
&targetRect, &targetRect, srcCopy, 0L);
}
//------------------------------------------------------------------------------------------\\
// End MGWGraphics1.c \\
//------------------------------------------------------------------------------------------\\